/* PlayAllVoices
 * Plays all voices with a random algorithmic tune
 * on the Modern Device Fluxamasynth Shield.
 */

#include <FlexamySynth.h>
#include <PgmChange.h>

FlexamySynth synth;

byte channel = 0;
byte bank = BANK_GM;
byte voice = 0;
byte j = 0;

byte minorScale[15] = {0,2,3,5,7,8,10, 12,14,15,17,19,20,22, 24 }; // 2 octaves
byte majorScale[15] = {0,2,4,5,7,9,11, 12,14,16,17,19,21,23, 24 }; // 2 octaves

#define BANK_NOTE  NOTE_C+OCTAVE*3 // *L.O.L*

void setup() 
{
  synth.begin();
  synth.midiReset();
  
  synth.setPatchBank( bank );
  synth.programChange( DRUM_CHAN, DRUMS_Power );
  synth.programChange( channel, GM_GrandPiano );
  synth.setBendRange( channel, 12 ); // Bend a whole octave
}

void playNote( byte Chan, byte Key, byte Velocity, word Duration )
{
    synth.noteOn( Chan, Key, Velocity );
    delay( Duration );
    synth.noteOff( Chan, Key );
}

void loop()
{
  synth.programChange( channel, voice );

  for( int i=0; i < 3; i++ )
  {
    byte note = majorScale[ random( 15 )] + OCTAVE*4;
    playNote( channel, note, 127, 110 );
    delay( 10 );
  }

  if (++voice > 127) {
    voice = 0;
    if (bank == BANK_GM) bank = BANK_MT32; 
    else bank = BANK_GM;
    synth.setPatchBank( bank );
    
    // Signal bank change
    byte patch = (bank == BANK_GM) ? GM_Applause : MT32_OneNoteJam;
    synth.programChange( 7, patch );
    playNote( 7, BANK_NOTE, 127, 3000 );
    delay( 100 );
  }
}
